/*
 * Copyright 2010-2013 MaMuN
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package main.java.net.bornil.db.service.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import main.java.net.bornil.db.entity.Event;
import main.java.net.bornil.db.entity.admin.User;
import main.java.net.bornil.db.service.UserDao;
import main.java.net.bornil.db.service.jdbc.JdbcEventDao.EventRowMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

/**
 * @author MaMuN
 * @since Sep 22, 2012
 */
@Repository
public class JdbcUserDao implements UserDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    final static class UserRowMapper implements RowMapper<User> {
        public User mapRow(ResultSet resultSet, int i) throws SQLException {
            User user = new User();
            user.setUserId(resultSet.getString("USERNAME"));
            user.setEnabled(resultSet.getBoolean("ENABLED"));

            return user;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * main.java.net.bornil.db.service.UserDao#createUser(main.java.net.bornil
     * .db.entity.User)
     */
    @Override
    public int createUser(User user) {
        SimpleJdbcInsert insertUser = new SimpleJdbcInsert(this.jdbcTemplate);
        insertUser.withTableName("USERS");
        insertUser.usingColumns("USERNAME", "PASSWORD", "ENABLED");

        Map<String, Object> args = new HashMap<String, Object>(2);
        args.put("USERNAME", user.getUserId());
        args.put("PASSWORD", user.getUserPass());
        args.put("ENABLED", true);

        insertUser.execute(args);

        return 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see main.java.net.bornil.db.service.UserDao#getUser(java.lang.String)
     */
    @Override
    public User getUser(String userId) {
        try {
            return this.jdbcTemplate.queryForObject("select USERNAME, ENABLED from USERS where USERNAME = ?",
                    new Object[] { userId }, new UserRowMapper());
        } catch (IncorrectResultSizeDataAccessException ex) {
            return new User();
        }

    }
}
